﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors.Controls;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing.Design;
using ProgNetDxComponents.Design.Editors;
using ProgNetComponents.Design.TypeConverters;
using ProgNetComponents.Design.Attributes;

namespace ProgNetDxComponents.ExtenderProviders.Language
{
    [TypeConverter(typeof(StandardDifferentCtorTypeConverter))]
    public class RadioItemLngInfo
    {
        internal RadioGroupItemCollection Collection;
        public RadioItemLngInfo(object value = null, string strCode = null)
        {
            this.ItemValue = value;
            this.StringCode = strCode;
        }

        [CtorParam(0,1,true)]
        public object ItemValue
        {
            get;
            set;
        }

        [CtorParam(1, 1, true)]
        public string StringCode
        {
            get;
            set;
        }

        public override string ToString()
        {
            return ItemValue.AsString() + " [" + StringCode + "]";
        }
    }

    [Editor(typeof(RadioItemLngInfoCollectionEditor), typeof(UITypeEditor))]
    public class RadioItemLngInfoCollection : Collection<RadioItemLngInfo>
    {
        private bool LockEvents = false;
        public event EventHandler CollectionModified;
        protected void OnCollectionModified()
        {
            if (!LockEvents)
            {
                if (CollectionModified != null)
                    CollectionModified(this, EventArgs.Empty);
            }
        }
        internal RadioGroupItemCollection Collection;
        protected override void ClearItems()
        {
            base.ClearItems();
            OnCollectionModified();
        }
        protected override void InsertItem(int index, RadioItemLngInfo item)
        {
            base.InsertItem(index, item);
            OnCollectionModified();
        }
        protected override void RemoveItem(int index)
        {
            base.RemoveItem(index);
            OnCollectionModified();
        }
        protected override void SetItem(int index, RadioItemLngInfo item)
        {
            base.SetItem(index, item);
            OnCollectionModified();
        }
        public void AddRange(RadioItemLngInfo[] items)
        {
            if (items.IsNotNull())
            {
                LockEvents = true;
                foreach (RadioItemLngInfo i in items)
                    Add(i);
                LockEvents = false;
                OnCollectionModified();
            }
        }
        internal void InitRadioSettings()
        {
            if (Collection != null)
            {
                List<RadioItemLngInfo> list = new List<RadioItemLngInfo>();
                foreach (RadioGroupItem item in Collection)
                {
                    bool contains = false;
                    foreach (RadioItemLngInfo info in this)
                    {
                        if (info.ItemValue.IsTheSame(item.Value))
                        {
                            contains = true;
                            break;
                        }
                    }
                    if (!contains)
                    {
                        RadioItemLngInfo i = new RadioItemLngInfo();
                        i.ItemValue = item.Value;
                        this.Add(i);
                    }
                }
                List<RadioGroupItem> l = new List<RadioGroupItem>();

                foreach (RadioGroupItem item in Collection)
                {
                    l.Add(item);
                }
                foreach (RadioItemLngInfo info in this)
                {
                    bool contains = false;
                    RadioGroupItem itemToDel = null;
                    foreach (RadioGroupItem item in l)
                    {
                        if (item.Value.IsTheSame(info.ItemValue))
                        {
                            itemToDel = item;
                            contains = true;
                            break;
                        }
                    }
                    if (itemToDel != null)
                    {
                        l.Remove(itemToDel);
                    }
                    if (!contains)
                        list.Add(info);
                }
                foreach (RadioItemLngInfo i in list)
                {
                    this.Remove(i);
                }
            }
            else
            {
                this.Clear();
            }
        }
    }
}
